Shader "Unlit/PaperWetEffect"
{
    Properties
    {
        _MainTex ("Dry Texture", 2D) = "white" {}
        _WetTex ("Wet Texture", 2D) = "white" {}
        _WetProgress ("Wet Progress", Range(0, 1)) = 0
        _WetDirection ("Wet Direction", Vector) = (0, 1, 0, 0)
        _WetSmoothness ("Wet Smoothness", Range(0.01, 0.5)) = 0.1
        _NoiseScale ("Noise Scale", Range(1, 20)) = 10
        _WetIntensity ("Wet Intensity", Range(0, 2)) = 1
        _Alpha ("Alpha", Range(0, 1)) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent" }
        LOD 100

        Pass
        {
            // 标准透明渲染设置
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            Cull Back
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _WetTex;
            float _WetProgress;
            float2 _WetDirection;
            float _WetSmoothness;
            float _NoiseScale;
            float _WetIntensity;
            float _Alpha;

            // 伪随机数生成函数
            float random(float2 st) {
                return frac(sin(dot(st.xy, float2(12.9898, 78.233))) * 43758.5453123);
            }

            // 2D噪声函数
            float noise(float2 st) {
                float2 i = floor(st);
                float2 f = frac(st);

                // 四个角的随机值
                float a = random(i);
                float b = random(i + float2(1.0, 0.0));
                float c = random(i + float2(0.0, 1.0));
                float d = random(i + float2(1.0, 1.0));

                // 平滑插值
                float2 u = f * f * (3.0 - 2.0 * f);

                // 混合结果
                return lerp(a, b, u.x) +
                    (c - a) * u.y * (1.0 - u.x) +
                    (d - b) * u.x * u.y;
            }

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // 生成噪声值
                float noiseValue = noise(i.uv * _NoiseScale);
                
                // 标准化湿润方向
                float2 normalizedDir = normalize(_WetDirection);
                
                // 将UV坐标转换为相对于原点的位置向量
                float2 uvVector = i.uv;
                
                // 计算当前点在湿润方向上的投影距离
                float projection = dot(uvVector, normalizedDir);
                
                // 计算起始点（通常是方向的反方向最远点）
                float2 startPoint = float2(0, 0);
                if (normalizedDir.x < 0) startPoint.x = 1;
                if (normalizedDir.y < 0) startPoint.y = 1;
                
                // 计算从起始点到当前点在方向上的投影
                float2 startToCurrent = uvVector - startPoint;
                float distanceFromStart = dot(startToCurrent, normalizedDir);
                
                // 计算最大可能的距离（从起始点到终点的距离）
                float2 endPoint = float2(1, 1);
                if (normalizedDir.x < 0) endPoint.x = 0;
                if (normalizedDir.y < 0) endPoint.y = 0;
                float maxDistance = dot(endPoint - startPoint, normalizedDir);
                
                // 归一化距离到0-1范围
                float normalizedDistance = distanceFromStart / maxDistance;
                
                // 加入噪声影响，使边缘不规则
                float wetThreshold = _WetProgress + noiseValue * 0.1;
                
                // 使用smoothstep创建不规则的湿润边缘
                float wetFactor = smoothstep(wetThreshold - _WetSmoothness, wetThreshold + _WetSmoothness, normalizedDistance);
                
                // 根据湿润因子混合干湿两种纹理
                fixed4 dryColor = tex2D(_MainTex, i.uv);
                fixed4 wetColor = tex2D(_WetTex, i.uv);
                
                // 增强湿润区域的颜色饱和度和亮度
                wetColor.rgb *= _WetIntensity;
                
                fixed4 finalColor = lerp(wetColor, dryColor, wetFactor);
                
                // 应用整体透明度
                finalColor.a *= _Alpha;
                
                return finalColor;
            }
            ENDCG
        }
    }
}
